package com.sps.business.service.impl;

import java.util.List;

import com.sps.business.domain.CcWarehouserecord;
import com.sps.business.domain.RemoveTemp;
import com.sps.business.mapper.CcWarehouserecordMapper;
import com.sps.common.exception.ServiceException;
import com.sps.common.utils.DateUtils;
import com.sps.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sps.business.mapper.CcStockinfoInfoMapper;
import com.sps.business.domain.CcStockinfoInfo;
import com.sps.business.service.ICcStockinfoInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 库存信息Service业务层处理
 * 
 * @author 王超
 * @date 2025-01-08
 */
@Service
public class CcStockinfoInfoServiceImpl implements ICcStockinfoInfoService 
{
    @Autowired
    private CcStockinfoInfoMapper ccStockinfoInfoMapper;

    @Autowired
    private CcWarehouserecordMapper ccWarehouserecordMapper;

    /**
     * 查询库存信息
     * 
     * @param id 库存信息主键
     * @return 库存信息
     */
    @Override
    public CcStockinfoInfo selectCcStockinfoInfoById(Long id)
    {
        return ccStockinfoInfoMapper.selectCcStockinfoInfoById(id);
    }

    /**
     * 查询库存信息列表
     * 
     * @param ccStockinfoInfo 库存信息
     * @return 库存信息
     */
    @Override
    public List<CcStockinfoInfo> selectCcStockinfoInfoList(CcStockinfoInfo ccStockinfoInfo)
    {
        return ccStockinfoInfoMapper.selectCcStockinfoInfoList(ccStockinfoInfo);
    }

    /**
     * 新增库存信息
     * 
     * @param ccStockinfoInfo 库存信息
     * @return 结果
     */
    @Override
    public int insertCcStockinfoInfo(CcStockinfoInfo ccStockinfoInfo)
    {
        ccStockinfoInfo.setCreateTime(DateUtils.getNowDate());
        return ccStockinfoInfoMapper.insertCcStockinfoInfo(ccStockinfoInfo);
    }

    /**
     * 修改库存信息
     * 
     * @param ccStockinfoInfo 库存信息
     * @return 结果
     */
    @Override
    public int updateCcStockinfoInfo(CcStockinfoInfo ccStockinfoInfo)
    {
        ccStockinfoInfo.setUpdateTime(DateUtils.getNowDate());
        return ccStockinfoInfoMapper.updateCcStockinfoInfo(ccStockinfoInfo);
    }

    /**
     * 批量删除库存信息
     * 
     * @param ids 需要删除的库存信息主键
     * @return 结果
     */
    @Override
    public int deleteCcStockinfoInfoByIds(Long[] ids)
    {
        return ccStockinfoInfoMapper.deleteCcStockinfoInfoByIds(ids);
    }

    /**
     * 删除库存信息信息
     * 
     * @param id 库存信息主键
     * @return 结果
     */
    @Override
    public int deleteCcStockinfoInfoById(Long id)
    {
        return ccStockinfoInfoMapper.deleteCcStockinfoInfoById(id);
    }

    /**
     * 扫码补货（库位暂时默认）
     * @param temp
     * @return
     */
    @Override
    @Transactional
    public int insertTemp(RemoveTemp temp) {
       // throw new ServiceException("操作异常，请联系管理员");
            int i = 0;
        //先查询库存中有无记录，有的话更新，没有则新增
        CcStockinfoInfo ccStockinfoInfo = ccStockinfoInfoMapper.selectCcStockinfoInfoByItemId(temp.getItemCode());
        if(ccStockinfoInfo==null){
            //2.向库存表中增加数据
            CcStockinfoInfo insertStockinfoInfo = new CcStockinfoInfo();
            insertStockinfoInfo.setDeptId(103L);
            insertStockinfoInfo.setItemId(temp.getItemCode());//物料号
            insertStockinfoInfo.setItemName(temp.getItemName());//物料名称
            insertStockinfoInfo.setKcNum(temp.getQlNum());//入库数量
            insertStockinfoInfo.setLockNum(0L);//锁帐初始默认为0
            insertStockinfoInfo.setQutlityStauts(0L);//质量状态，默认都合格
            insertStockinfoInfo.setSupplyCode(temp.getSuppCode());
            insertStockinfoInfo.setSupplyName(temp.getSuppName());
            insertStockinfoInfo.setKwCode("sps-001");//库位编号暂定写死
            insertStockinfoInfo.setKwName("sps皮卡");//库位名称 暂定写死
            insertStockinfoInfo.setLableCode(temp.getLableCode());//标签号
            insertStockinfoInfo.setApplianceyCode(temp.getApplianceyCode());//器具位置号
            insertStockinfoInfo.setApplianceyName(temp.getApplianceyName());//器具名称
            insertStockinfoInfo.setSpsArea(temp.getSpsArea());//sps区
            insertStockinfoInfo.setUnit(temp.getUnit());//单位
            insertStockinfoInfo.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());//创建人
            insertStockinfoInfo.setCreateTime(DateUtils.getNowDate());//创建时间
            insertStockinfoInfo.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());//更新人
            insertStockinfoInfo.setUpdateTime(DateUtils.getNowDate());//更新时间
            i = ccStockinfoInfoMapper.insertCcStockinfoInfo(insertStockinfoInfo);
        }else{
            //更新数量
            ccStockinfoInfo.setKcNum(ccStockinfoInfo.getKcNum()+temp.getQlNum());
            ccStockinfoInfo.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());//更新人
            ccStockinfoInfo.setUpdateTime(DateUtils.getNowDate());//更新时间
            i = ccStockinfoInfoMapper.updateCcStockinfoInfo(ccStockinfoInfo);
        }

        //2.向流水表中增加数据
        CcWarehouserecord ccWarehouserecord = new CcWarehouserecord();
        ccWarehouserecord.setItemName(temp.getItemName());
        ccWarehouserecord.setItemId(temp.getItemCode());
        ccWarehouserecord.setBsinssType(0L);
        ccWarehouserecord.setCrNum(temp.getQlNum());
        ccWarehouserecord.setSupplyCode(temp.getSuppCode());
        ccWarehouserecord.setSupplyName(temp.getSuppName());
        ccWarehouserecord.setUnit(temp.getUnit());
        ccWarehouserecord.setKwCode("sps-001");
        ccWarehouserecord.setKwName("sps皮卡");
        ccWarehouserecord.setLableCode(temp.getLableCode());
        ccWarehouserecord.setApplianceyCode(temp.getApplianceyCode());
        ccWarehouserecord.setApplianceyName(temp.getApplianceyName());
        ccWarehouserecord.setSpsArea(temp.getSpsArea());
        ccWarehouserecord.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());//创建人
        ccWarehouserecord.setCreateTime(DateUtils.getNowDate());//创建时间
        ccWarehouserecord.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());//更新人
        ccWarehouserecord.setUpdateTime(DateUtils.getNowDate());//更新时间
        ccWarehouserecordMapper.insertCcWarehouserecord(ccWarehouserecord);
        return i;
    }
}
